﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CommonMethod
{
    public static class CSVHelper
    {
        #region 将list转换为CSV
        /// <summary>
        /// 将list转换为CSV
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="lst">列表</param>
        /// <param name="filePath">保存的csv文件路径</param>
        /// <param name="colname">列名数组</param>
        /// <returns></returns>
        public static bool SaveListtoCsv<T>(List<T> lst, string filePath, string[] colname)
        {
            using (StreamWriter sw = new StreamWriter(filePath))
            {
                MemoryStream memoryStream = new MemoryStream();
                // 生成列
                var type = typeof(T);
                PropertyInfo[] props = type.GetProperties();
                StringBuilder strColumn = new StringBuilder();
                foreach (string item in colname)
                {
                    strColumn.Append(item);
                    strColumn.Append(",");
                }
                sw.WriteLine(strColumn);

                // 写入数据
                StringBuilder strValue = new StringBuilder();
                foreach (var dr in lst)
                {
                    strValue.Clear();
                    foreach (PropertyInfo item in props)
                    {
                        strValue.Append(item.GetValue(dr));
                        strValue.Append(",");
                    }
                    sw.WriteLine(strValue);
                }
                return true;
            }
        }
        #endregion
        /// <summary>
        /// 将列表生成表格文件流
        /// </summary>
        /// <typeparam name="T">要生成表格的类</typeparam>
        /// <param name="lst">数据</param>
        /// <param name="colname">列名，注意顺序</param>
        /// <returns></returns>
        public static MemoryStream SaveListtoCsv<T>(List<T> lst, string[] colname)
        {

            MemoryStream memoryStream = new MemoryStream();
            StreamWriter sw = new StreamWriter(memoryStream);
            // 生成列
            var type = typeof(T);
            PropertyInfo[] props = type.GetProperties();
            StringBuilder strColumn = new StringBuilder();
            foreach (string item in colname)
            {
                strColumn.Append(item);
                strColumn.Append(",");
            }
            strColumn.Append("\r\n");
            byte[] buffer = Encoding.Default.GetBytes(strColumn.ToString());
            memoryStream.Write(buffer, 0, buffer.Length);
            // 写入数据
            StringBuilder strValue = new StringBuilder();
            foreach (var dr in lst)
            {
                strValue.Clear();
                foreach (PropertyInfo item in props)
                {
                    strValue.Append(item.GetValue(dr));
                    strValue.Append(",");
                }
                strValue.Append("\r\n");
                buffer = Encoding.Default.GetBytes(strValue.ToString());
                memoryStream.Write(buffer, 0, buffer.Length);
            }
            memoryStream.Seek(0, SeekOrigin.Begin);
            return memoryStream;

        }
    }
}
